package com.stephen.jsbridge.xwalkbridge;

import android.app.Activity;
import android.content.Context;
import android.net.http.SslError;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.webkit.ValueCallback;

import com.stephen.jsbridge.jsbridge.OnWebViewListener;

import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by stephen on 10/24/17.
 */

public class XWalkWebChromeClient extends XWalkResourceClient{
    private Activity activity;
    private XWalkWebView webView;
    private OnWebViewListener onWebViewListener;

    public XWalkWebChromeClient(Activity activity, XWalkWebView view, OnWebViewListener onWebViewListener) {
        super(view);
        this.webView = view;
        this.activity = activity;
        this.onWebViewListener = onWebViewListener;
    }

    @Override
    public void onLoadStarted(XWalkView view, String url) {
        super.onLoadStarted(view, url);
    }

    @Override
    public void onLoadFinished(XWalkView view, String url) {
        super.onLoadFinished(view, url);
    }

    @Override
    public void onProgressChanged(XWalkView view, int progressInPercent) {
        super.onProgressChanged(view, progressInPercent);
    }

    @Override
    public void onReceivedSslError(XWalkView view, ValueCallback<Boolean> callback, SslError error) {
        //super.onReceivedSslError(view, callback, error);
        callback.onReceiveValue(true);
        //if(null != onWebViewListener)onWebViewListener.onReceivedError(error.getPrimaryError(), error.toString(), error.getUrl());
    }

    @Override
    public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
        if(TextUtils.isEmpty(url))return super.shouldOverrideUrlLoading(view, url);

        try{
            url = URLDecoder.decode(url, "UTF-8");
        }catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if(url.startsWith(BridgeUtil.YY_RETURN_DATA)){//如果是返回数据
            webView.handlerReturnData(url);
            return true;
        }else if(url.startsWith(BridgeUtil.YY_OVERRIDE_SCHEMA)){
            webView.flushMessageQueue();
            return true;
        }else{
            if(!url.contains("tel:"))url += ("?T="+ System.currentTimeMillis());
            System.out.println("==========shouldOverrideUrlLoading Url===>"+url);
            hideSystemSoftInputMethod();
            if(null != onWebViewListener)return onWebViewListener.onOverrideUrlLoading(view,url);
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

    @Override
    public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {
        if(null != onWebViewListener)onWebViewListener.onReceivedError(view, errorCode, description, failingUrl);
        super.onReceivedLoadError(view, errorCode, description, failingUrl);
    }

    private void hideSystemSoftInputMethod(){
        if(null != activity && null != activity.getCurrentFocus() && null != activity.getCurrentFocus().getWindowToken()){//先隐藏前个页面的打开的输入法,避免因为打开新页面后输入法再隐藏导致新页面渲染高度出现错误
            ((InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }//end of if
    }
}
